﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.APIRequestModels.SysUser;
using Ekton_Manage_DTO.RequestModels.SysUser;
using Ekton_Manage_DTO.RequestModels.User;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;


namespace Ekton_Manage_Business
{
    public class APIRegster : IApiUserLogin
    {

        private readonly SerializeParameter _serialize;
        private readonly IBaseRepository<user_list, int> _baseMaterialRepository;
        private readonly IBaseRepository<verify_code, int> _baseverifyCodeRepository;
        private readonly IBaseRepository<user_list, int> _repository;
        public APIRegster(SerializeParameter serialize, IBaseRepository<user_list, int> baseMaterialRepository, IBaseRepository<verify_code, int> baseverifyCodeRepository, IBaseRepository<user_list, int> repository)
        {

            _serialize = serialize;
            _baseMaterialRepository = baseMaterialRepository;
            _baseverifyCodeRepository = baseverifyCodeRepository;
            _repository = repository;
        }


        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="prouderType"></param>
        /// <returns></returns>
        public async Task<Message> ApiPostUserRegsiter(ApiDtoInsertUserRegister request)
        {
            var verifyCode = await _baseverifyCodeRepository.QuerySingleData(m => m.email == request.email && m.state == 0);
            var userName = await _baseMaterialRepository.QuerySingleData(m => m.account == request.account);
            if (verifyCode is null)
            {
                return new Message("输入的是无效的邮箱或是无效的验证码，请仔细核对", false, 202);
            }
            if (userName is null)
            {
                if (DateTime.Now <= verifyCode.create_op_date.AddMinutes(50))
                {
                    if (request.verify_code == verifyCode.code)
                    {
                        var userInfo = await _baseMaterialRepository.InsertSingleData(new user_list
                        {
                            account = request.account,
                            password = request.password,
                            name = request.name,
                            sex = request.sex,
                            phone = request.phone,
                            email = request.email,
                            company = request.company
                        });
                        //添加用户成功后修改验证码状态
                        verifyCode.state = 1;
                        var updateCodeState = await _baseverifyCodeRepository.UpdateSingleData(verifyCode);
                        return new Message(userInfo.Item1);
                    }
                    else
                    {
                        return new Message("无效的验证码，请仔细核对", false, 202);
                    }
                }
                else
                {
                    //超时后修改验证码状态
                    verifyCode.state = 1;
                    var updateCodeState = await _baseverifyCodeRepository.UpdateSingleData(verifyCode);
                    return new Message("验证码已超时", false, 202);
                }
            }
            else
            {
                return new Message("该用户名已被注册", false, 202);
            }
        }

        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="prouderType"></param>
        /// <returns></returns>
        public async Task<Message> ApiPostUserPass(ApiDtoInsertUserPass request)
        {
            var verifyCode = await _baseverifyCodeRepository.QuerySingleData(m => m.email == request.email&& m.state ==0);
            var userName = await _baseMaterialRepository.QuerySingleData(m => m.account == request.account);
            if (verifyCode is null)
            {
                return new Message("输入的是无效的邮箱或是无效的验证码，请仔细核对", false, 202);
            }
            if (userName is null)
            {
                return new Message("输入的用户名不正确", false, 202);
            }
            else
            {
                if (request.auth_code == verifyCode.code)
                {
                    if (DateTime.Now <= verifyCode.create_op_date.AddMinutes(5))
                    {
                        var user = await _repository.QuerySingleData(m => m.account == request.account && m.email == request.email);
                        if (user is null) return new Message("该用户不存在", false, 202);
                        user.password = request.password;
                        var result = await _repository.UpdateSingleData(user);
                        //修改密码成功后修改验证码状态
                        verifyCode.state = 1;
                        var updateCodeState = await _baseverifyCodeRepository.UpdateSingleData(verifyCode);
                        return new Message(result.Item1);
                    }
                    else
                    {
                        //验证码超时后修改验证码状态
                        verifyCode.state = 1;
                        var updateCodeState = await _baseverifyCodeRepository.UpdateSingleData(verifyCode);
                        return new Message("验证码已超时", false, 202);
                    }
                }
                else
                {
                    return new Message("无效的验证码，请仔细核对", false, 202);
                }
            }

        }


    }
}
